class SearchController < ApplicationController
  before_filter :authorize
  
  def index
    @query = params[:query]
    searchables = ['semantic_type','description','tag','category','all']
    @menu_options = ''
    searchables.each { |s| @menu_options << s.titleize }
    if @query.blank?
      respond_to do |format|
        format.html # index.html.erb
        format.xml { render :layout => false  } # index.xml.builder
      end      
    else
      @results = find_results
      respond_to do |format|
        format.html # index.html.erb
        format.xml { render :layout => false  } # index.xml.builder
      end          
    end
  end
  
  def show
    @query = params[:query]
    if @query.blank?
      respond_to do |format|
        format.html # show.html.erb
        format.xml { render :layout => false  } # show.xml.builder
      end      
    else
      @results = find_results
      respond_to do |format|
        format.html # show.html.erb
        format.xml { render :layout => false  } # show.xml.builder
      end          
    end
  end
  
  def shim_finder
    #Make all checkboxes checked, unless they have been unchecked by user
    @box = Hash.new
    @box.default = true unless params[:show]
    params[:show].each {|key,val| @box[key] = val} unless params[:show].nil?
    
    @source = params[:source_service_id]
    @dest = params[:destination_service_id]
    
    if @source.blank? && @dest.blank?
      respond_to do |format|
        format.html # shim_finder.html.erb
        format.xml { render :layout => false  } # shim_finder.xml.builder
      end
    else
      @results = find_shims_by_linked_services.paginate :page => params[:page], :per_page => 10
      respond_to do |format|
        format.html # shim_finder.html.erb
        format.xml { render :layout => false  } # shim_finder.xml.builder
      end          
    end
  end
  
  protected
  
  def find_results
    count = Shim.count_by_solr(params[:query])
    @results_count = count
    return Shim.paginate_all_by_solr(params[:query], :page => params[:page], :per_page => 10, :total_entries => count)
  end
  
  def find_shims_by_linked_services
    matches = Array.new    
    input_matches = Array.new
    output_matches = Array.new    
  
    #Get all shims with inputs linked to the source service
    unless @source.blank?
      sources = ServiceLink.find(:all, :conditions => {:service_id => @source, :port_type => "ShimInput"})
      sources.each do |s|
        shim = Shim.find_by_id(ShimInput.find_by_id(s.port_id).shim_id)
        #Only add if user wants
        input_matches << shim if @box[shim.shim_type]
      end
    end

    #Get all shims with outputs linked to the destination service
    unless @dest.blank?     
      destinations = ServiceLink.find(:all, :conditions => {:service_id => @dest, :port_type => "ShimOutput"})
      destinations.each do |d|
        shim = Shim.find_by_id(ShimOutput.find_by_id(d.port_id).shim_id)
        output_matches << shim if @box[shim.shim_type]
      end 
    end    
    
    #Remove duplicate results
    input_matches.uniq!
    output_matches.uniq!   
    
    if sources && destinations
      #Get the intersection of both
      matches = input_matches & output_matches         
    elsif sources
      #Get just the source matches
      matches = input_matches
    elsif destinations
      #Get just the destination matches
      matches = output_matches
    end       
    
    @results_count = matches.length
    return matches
  end
  
  def authorize
  end
end